<?php

/**
 * @file
 * The main application file.
 *
 * Status:
 * -0 New Loss
 * -1 Ignored Loss
 * -2 Submitted Loss
 * -3 Completed Loss
 * -4 Denied Loss
 *
 * @copyright 2010 David Hinkle
 * @author David Hinkle
 * @license GPL
 *
 */
 
include_once("security.php");

// Get all new kills from killboard
$kb = new killboard($conn, $killboardUrl);
while (($ret = $kb->parse()) > 0);

if (isset($_GET['q']))
{
	if ($_GET['q'] == 'ignore')
		{
		$query = sprintf("update losses set status = 1 where killId = %s", mysql_real_escape_string($_GET['killId']));
		mysql_query($query, $conn);
		header("location:index.php");
		}

	if ($_GET['q'] == 'submit')
		{
		$query = sprintf("update losses set status = 2, cloneGrade = '%s' where killId = %s", $UserData['clone'], mysql_real_escape_string($_GET['killId']) );
		mysql_query($query, $conn);
		header("location:index.php");
		}

	if ($_GET['q'] == 'cancel')
		{
		$query = sprintf("update losses set status = 0 where killId = %s", mysql_real_escape_string($_GET['killId']));
		mysql_query($query, $conn);
		header("location:index.php");
		}


	if ($_GET['q'] == 'refresh')
	{
		if ($eve->pilotName != "")
		{
			// Get all new kills from killboard
			$kb = new killboard($conn, $killboardUrl);
			while (($ret = $kb->parse_player($eve->pilotName)) > 0);			
		}
	}
}

if (isset($_GET['submit']) && $_GET['submit'] == "Select Clone" && isset($_SERVER['HTTP_EVE_CHARID']))
{
	$query = sprintf("update players set clone = '%s' where charid = %d", mysql_real_escape_string($_GET['clone']), mysql_real_escape_string($_SERVER['HTTP_EVE_CHARID']));
	mysql_query($query, $conn);
	header("location:index.php");
}

require("header.php");

if (@$_GET['q'] == 'refresh')
{
	echo "<div class='message'>Missing losses:<br/>".$kb->getMessage()."</div>";
}


if (@$_GET['q'] == "about")
	{
?>
<BR><BR><BR><BR><BR><BR><BR><center><table width=80%><tr><td>
Tgrads kill reimbursement claim processing system.<br>
<br>
&copy; 2010 David Hinkle aka Drachs<br>
<BR>
Licensed via the GPL
</table>
<?
	exit;
	}


/***************************************************************************************
 * Select Clone Grade
 ***************************************************************************************/


if (@$_GET['q'] == "clonegrade")
{
	?>
	<center>
	<br><br>
	<form method="get">
	<select name=clone>
	<option value='Alpha'>Clone Grade Alpha</option>
	<option value='Beta'>Clone Grade Beta</option>
	<option value='Gamma'>Clone Grade Gamma</option>
	<option value='Delta'>Clone Grade Delta</option>
	<option value='Epsilon'>Clone Grade Epsilon</option>
	<option value='Zeta'>Clone Grade Zeta</option>
	<option value='Eta'>Clone Grade Eta</option>
	<option value='Theta'>Clone Grade Theta</option>
	<option value='Iota'>Clone Grade Iota</option>
	<option value='Kappa'>Clone Grade Kappa</option>
	<option value='Lambda'>Clone Grade Lambda</option>
	<option value='Mu'>Clone Grade Mu</option>
	<option value='Nu'>Clone Grade Nu</option>
	<option value='Xi'>Clone Grade Xi</option>
	<option value='Omicron'>Clone Grade Omicron</option>
	<option value='Pi'>Clone Grade Pi</option>
	<option value='Rho'>Clone Grade Rho</option>
	<option value='Sigma'>Clone Grade Sigma</option>
	<option value='Tau'>Clone Grade Tau</option>
	<option value='Upsilon'>Clone Grade Upsilon</option>
	<option value='Phi'>Clone Grade Phi</option>
	<option value='Chi'>Clone Grade Chi</option>
	<option value='Psi'>Clone Grade Psi</option>
	<option value='Omega'>Clone Grade Omega</option>
	</select>
	<input type=submit value="Select Clone" name=submit>
	</form>
	<?php
	exit;
}

/***************************************************************************************
 * Display Ignored Losses
 ***************************************************************************************/

if (@$_GET['q'] == "ignored")
{
	$query = sprintf("select * from losses where victim = '%s' and status = 1", mysql_real_escape_string($eve->pilotName));
	$res = mysql_query($query, $conn);
	if (!$res)
		die("Problem with mysql: ".$message);

	if (mysql_num_rows($res) == 0)
		$Note = "The killboard has no record of any ignored losses for ".$eve->pilotName;
	else
		$Note = "The following losses have been ignored:";
	?>
	<center>
	<br><br>
	<table width=80%>
	<tr><td><?=$Note?>
	</table>
	<?
	echo("<br><br><table width=80%>");
	while($row = mysql_fetch_assoc($res))
	{
		echo("<TR>");
		echo("<TD><img src=\"images/".$row['shipId'].".png\">");

		echo("<TD><Table>");
		echo("<TR><TD>Ship: ".$row['ship']);
		echo("<TR><TD>System: ".$row['system']);
		echo("<TR><TD>Killer: ".$row['killer']);
		echo("<TR><TD>Alliance: ".$row['killerAlliance']);
		echo("</table>");
		echo("<TD><A href=".$killboardUrl."/?a=kill_detail&kll_id=".$row['killId'].">View</a><br>");
		echo("<br><a href=index.php?q=submit&killId=".$row['killId'].">Submit</a>");
		echo("\n");
	}
	echo("</table>");
	exit;
}

/****************************************************************************************
 * Display Submitted but not processed losses
 ****************************************************************************************/

$query = sprintf("select * from losses where victim = '%s' and status = 2", mysql_real_escape_string($eve->pilotName));
$res = mysql_query($query, $conn);
if (!$res)
	die("Problem with mysql: ".$message);

if (mysql_num_rows($res) == 0)
	$Note = "The killboard has no record of any pending reimbursement claims for ".$eve->pilotName;
else
	$Note = "The following claims for reimbursement are pending:";
?>
<center>
<br><br>
<a href="index.php?q=refresh">Check for missing losses</a>

<table width=80%>
<tr><td><?=$Note?>
</table>
<?
	echo("<br><br><table width=80%>");
	while($row = mysql_fetch_assoc($res))
	{
		echo("<TR>");
		echo("<TD><img src=\"images/".$row['shipId'].".png\">");	
		echo("<TD><Table>");
		echo("<TR><TD>Ship: ".$row['ship']);
		if ($row['ship'] == 'Capsule')
			echo("<br>Clone Grade: ".$row['cloneGrade']);
		echo("<TR><TD>System: ".$row['system']);
		echo("<TR><TD>Killer: ".$row['killer']);
		echo("<TR><TD>Alliance: ".$row['killerAlliance']);
		echo("</table>");
		echo("<TD><A href=".$killboardUrl."//?a=kill_detail&kll_id=".$row['killId'].">View</a><br>");
		echo("<br><A href=index.php>Ignore</a><br>");
		echo("<br><a href=index.php?q=cancel&killId=".$row['killId'].">Cancel</a>");
		echo("\n");
	}
	echo("</table>");

/****************************************************************************************
 * Display recent losses
 ****************************************************************************************/
 
	$query = sprintf("select * from losses where victim = '%s' and status = 0", mysql_real_escape_string($eve->pilotName));
	$res = mysql_query($query, $conn);
	if (!$res)
		die("Problem with mysql: ".$message);
	
	if (mysql_num_rows($res) == 0)
		$Note = "The killboard has no record of any unprocessed losses for ".$eve->pilotName;
	else
		$Note = "This killboard has recorded the following losses:";
?>
<center>
<br><br>
<table width=80%>
<tr><td><?=$Note?>
</table>
<?
	echo("<br><br><table width=80%>");
	while($row = mysql_fetch_assoc($res))
	{
		echo("<TR>");
		echo("<TD><img src=\"images/".$row['shipId'].".png\">");
	
		echo("<TD><Table>");
		echo("<TR><TD>Ship: ".$row['ship']);
		if ($row['ship'] == 'Capsule')
		{
			echo("<br>Clone Grade: ".$UserData['clone']);
			echo("<br><a href=index.php?q=clonegrade>Select Clone Grade</a>");
		}
		echo("<TR><TD>System: ".$row['system']);
		echo("<TR><TD>Killer: ".$row['killer']);
		echo("<TR><TD>Alliance: ".$row['killerAlliance']);
		echo("</table>");
		echo("<TD><A href=".$killboardUrl."//?a=kill_detail&kll_id=".$row['killId'].">View</a><br>");
		echo("<br><A href=index.php?q=ignore&killId=".$row['killId'].">Ignore</a><br>");
		echo("<br><a href=index.php?q=submit&killId=".$row['killId'].">Submit</a>");
		echo("\n");
	}
	echo("</table>");
?>